﻿using System.Collections.Generic;
using System.Linq;
using FairyGUI;
using UnityEngine;

namespace Whoot.UI
{
    public class UILayerManager : IUILayerManager
    {
        private IUIKit UIKit = null;

        public void SetUIKit(IUIKit kit)
        {
            UIKit = kit;
        }

        private class UILayerData
        {
            public UILayerData(int sort, GComponent t)
            {
                SortingOrder = sort;
                Root = t;
            }

            public int SortingOrder;
            public GComponent Root;
        }

        private readonly Dictionary<string, UILayerData> _uiLayers = new Dictionary<string, UILayerData>();

        private readonly List<UILayerData> _uiLayersList = new List<UILayerData>();
        private string _defLayerName = string.Empty;


        public void AddLayer(string name, int sortingOrder, bool isDefault = false)
        {
            if (_uiLayers.ContainsKey(name))
            {
                Debug.LogError("添加重复的Layer");
                return;
            }

            if (isDefault)
            {
                _defLayerName = name;
            }

            GComponent layer = new GComponent {name = name, gameObjectName = name};
            FairyGUI.GRoot.inst.AddChild(layer);
            layer.xy = Vector2.zero;
            layer.scale = Vector2.one;
            UILayerData data = new UILayerData(sortingOrder, layer);
            _uiLayers[name] = data;
            _uiLayersList.Add(data);
            RefreshLayer();
        }

        public void RemoveLayer(string name)
        {
            _uiLayers.TryGetValue(name, out var layer);
            if (layer != null)
            {
                _uiLayers.Remove(name);
                _uiLayersList.Remove(layer);
            }

            RefreshLayer();
        }
        
        public void AddChild(IUIPanel panel)
        {
            CheckNeedAddDefLayer();
            var layer = GetPanelLayer(panel);
            if (layer != null)
            {
                var node = panel.GetPanelNode() as GObject;
                layer.Root.AddChild(panel.GetPanelNode() as GObject);
                //layer.panels.Add(panel);
            }
            else
            {
                Debug.LogError("请设置默认Layer");
            }
        }


        public void RemoveChild(IUIPanel panel)
        {
            var layer = GetPanelLayer(panel);
            if (layer != null)
            {
                layer.Root.RemoveChild(panel.GetPanelNode() as GObject);
                //layer.panels.Remove(panel);
            }
            else
            {
                Debug.LogError("请设置默认Layer");
            }
        }

        public void Top(IUIPanel panel)
        {
            var layer = GetPanelLayer(panel);
            if (layer != null)
            {
                var pan = panel.GetPanelNode() as GObject;
                var node = layer.Root;
                var cnt = node.numChildren;
                int i = cnt - 1;
                
                if (i >= 0)
                {
                    node.SetChildIndex(pan, i);
                }
            }
        }

        private UILayerData GetPanelLayer(IUIPanel panel)
        {
            var layerName = panel.UILayer;
            _uiLayers.TryGetValue(layerName, out var layer);
            if (layerName.Equals(string.Empty))
            {
                layer = _uiLayers[_defLayerName];
            }

            return layer;
        }

        private void RefreshLayer()
        {
            _uiLayersList.Sort((a, b) => a.SortingOrder - b.SortingOrder);
            for (var i = 0; i < _uiLayersList.Count; i++)
            {
                var tran = _uiLayersList[i].Root;
                tran.sortingOrder = i;
                tran.displayObject.cachedTransform.SetSiblingIndex(i);
            }
        }

        private void CheckNeedAddDefLayer()
        {
            if (_uiLayers.Count > 0) return;
            AddLayer("def", 0, true);
        }
    }
}